﻿using AutoMapper;
using B.S.DataBase.Myproject.Helper;
using B.S.DataBase.MyProject.Domain.OutboundShipments;
using B.S.DataBase.MyProject.Domain.Sales;
using B.S.DataBase.MyProject.Domain.Storages;
using B.S.DataBase.MyProject.Domain.ViewModels;
using B.S.DataBase.MyProject.Domain.WhereHorse;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Read.API.Applicantions.Command.PurchaseInStroeCommand;

namespace T.E.ERP.Read.API.Applicantions.CommandHandler.PurchaseInStroeCommandHandler
{
    public class GetPurchaseInListCommandHandler : IRequestHandler<GetPurchaseInRepositoryListCommand, APIResult<List<View_PurchaseInList>>>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<BaseStorage> storeBiz;
        private readonly IBaseRepository<PurchaseInRepository> purchaseInBiz;
        private readonly IBaseRepository<OrderMaterials> orderMaterialsBiz;

        public GetPurchaseInListCommandHandler(IMapper mapper, IBaseRepository<BaseStorage> storeBiz, IBaseRepository<PurchaseInRepository> purchaseInBiz, IBaseRepository<OrderMaterials> orderMaterialsBiz)
        {
            this.mapper = mapper;
            this.storeBiz = storeBiz;
            this.purchaseInBiz = purchaseInBiz;
            this.orderMaterialsBiz = orderMaterialsBiz;

        }

        public Task<APIResult<List<View_PurchaseInList>>> Handle(GetPurchaseInRepositoryListCommand request, CancellationToken cancellationToken)
        {
            var store = storeBiz.GetAll().Result;
            var PurchaseIn = purchaseInBiz.GetAll().Result;
            var orderMaterials = orderMaterialsBiz.Quary(x => x.MaterialsClass == 2).Result;
            var list = from storeT in store
                       join PurchaseInT in PurchaseIn
                       on storeT.Cid equals PurchaseInT.Repository
                       join orderMaterialsT in orderMaterials
                       on PurchaseInT.Id equals orderMaterialsT.SaleCode
                       select new { storeT, PurchaseInT, orderMaterialsT };
            if (request.Id.IsNotNull())
            {
                list = list.Where(x => x.PurchaseInT.Id.ToString() == request.Id);
            }
            if (request.MaterialsName.IsNotNull())
            {
                list = list.Where(x => x.orderMaterialsT.MaterialsName == request.MaterialsName);
            }
            if (request.MaterialsCode.ToString().IsNotNull())
            {
                list = list.Where(x => x.orderMaterialsT.MaterialsCode == request.MaterialsCode);
            }
            if (request.Repository != 0)
            {
                list = list.Where(x => x.PurchaseInT.Repository == request.Repository);
            }
            APIResult<List<View_PurchaseInList>> result = new APIResult<List<View_PurchaseInList>>();
            result.Count = list.Count();
            result.data = list.Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).OrderByDescending(a => a.PurchaseInT.DocumentDate).Select(c =>
            new View_PurchaseInList
            {
                //单据编码
                Id = c.PurchaseInT.Id,
                //业务类型
                BusinessType = c.PurchaseInT.BusinessType,
                //业务状态
                BusinessStatus = c.PurchaseInT.BusinessStatus,
                //单据类型
                DocumentType = c.PurchaseInT.DocumentType,
                //单据状态
                DocumentStatus = c.PurchaseInT.DocumentStatus,
                //单据日期
                DocumentDate = c.PurchaseInT.DocumentDate.DateTimeToString("yyyy-MM-dd HH:mm:ss", "--"),
                //审批人姓名
                ExamUserName = c.PurchaseInT.ExamUserName,
                //物料名称
                MaterialsName = c.orderMaterialsT.MaterialsName,
                //物料编码
                MaterialsCode = c.orderMaterialsT.MaterialsCode,
                //物料id
                MaterialsId = c.orderMaterialsT.Id,
                //仓库id
                Repository = c.storeT.Cid,
                //仓库代码
                BaseCode = c.storeT.BaseCode,
                //仓库名称
                BaseName = c.storeT.BaseName

    }).ToList();
            result.Code = APIEnum.成功;
            result.Msg = APIEnum.成功.ToString();
            return Task.FromResult(result);
        }
    }
}
